﻿using BW.WMS.Application.Features.Bins.Dtos;
using BW.WMS.Application.Features.Bins.Interfaces;
using BW.WMS.Core.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BW.WMS.API.Controllers
{
    /// <summary>
    /// WMS仓库管理—货位控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WMS_BinController : ControllerBase
    {
        private readonly IWMS_BinService binService;
        /// <summary>
        ///通过构造函数注入货位服务接口层
        /// </summary>
        /// <param name="binService">货位服务接口层</param>
        public WMS_BinController(IWMS_BinService binService)
        {
            this.binService = binService;
        }
        /// <summary>
        /// 获取货位分页列表
        /// </summary>
        /// <param name="queryDto">查询条件</param>
        /// <returns>货位分页列表</returns>
        [HttpGet]
        public async Task<ApiPage<WMS_BinDto>> GetBinPageList([FromQuery] WMS_BinQueryDto queryDto)
        {
            return await binService.GetBinPageListAsync(queryDto);
        }

        /// <summary>
        /// 获取货位详情
        /// </summary>
        /// <param name="id">货位ID</param>
        /// <returns>货位详情</returns>
        [HttpGet]
        public async Task<ApiResult<WMS_BinDto>> GetBinById(long id)
        {
            return await binService.GetBinByIdAsync(id);
        }

        /// <summary>
        /// 创建货位
        /// </summary>
        /// <param name="createDto">创建货位DTO</param>
        /// <returns>新创建的货位ID</returns>
        [HttpPost]
        public async Task<ApiResult<long>> CreateBin([FromBody] WMS_BinCreateDto createDto)
        {
            return await binService.CreateBinAsync(createDto);
        }

        /// <summary>
        /// 更新货位
        /// </summary>
        /// <param name="updateDto">更新货位DTO</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        public async Task<ApiResult<bool>> UpdateBin([FromBody] WMS_BinUpdateDto updateDto)
        {
            return await binService.UpdateBinAsync(updateDto);
        }

        /// <summary>
        /// 删除货位
        /// </summary>
        /// <param name="id">货位ID</param>
        /// <returns>操作结果</returns>
        [HttpDelete]
        public async Task<ApiResult<bool>> DeleteBin(long id)
        {
            return await binService.DeleteBinAsync(id);
        }

        /// <summary>
        /// 批量删除货位
        /// </summary>
        /// <param name="ids">货位ID列表</param>
        /// <returns>操作结果</returns>
        [HttpDelete]
        public async Task<ApiResult<bool>> BatchDeleteBins([FromBody] List<long> ids)
        {
            return await binService.BatchDeleteBinsAsync(ids);
        }

        /// <summary>
        /// 更新货位状态
        /// </summary>
        /// <param name="id">货位ID</param>
        /// <param name="status">新状态值</param>
        /// <param name="updatedBy">操作人</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        public async Task<ApiResult<bool>> UpdateBinStatus(long id, int status, [FromQuery] string updatedBy)
        {
            return await binService.UpdateBinStatusAsync(id, status, updatedBy);
        }
    }
}
